﻿using System.Windows;
using Sinacor.Infra.UI.Controls.SmartClient;
using Sinacor.UI.Bovespa.Contabil.Presenter;
using System;
using System.Collections.Generic;
using Sinacor.Infra.UI.Authorization;

namespace Sinacor.UI.Bovespa.Contabil.View.SmartClient
{
    /// <summary>
    /// Interaction logic for CadastroContasContabeis.xaml
    /// </summary>
    public partial class CadastroContasContabeis : SinacorWindow, ICadastroContaContabilFaturamento
    {
        private Boolean _isSelectedEventoGrid = false;
        private Boolean _permitirNovaPesquisa = true;

        /// <summary>
        /// Propriedade com a presenter da view
        /// </summary>
        public Presenter.CadastroContaContabilFaturamento PresenterCadastroContaContabilFaturamento { get; set; }

        public Model.ContaContabilFaturamento CurrentModel
        {
            get
            {
                return (Model.ContaContabilFaturamento)this.DtgContasContabeis.EditItem;
            }
        }

        #region Construtor

        public CadastroContasContabeis()
        {
            InitializeComponent();

            this.KeyDown += new System.Windows.Input.KeyEventHandler(CadastroContasContabeis_KeyDown);
            this.DtgContasContabeis.NewItemType = typeof(Model.ContaContabilFaturamento);
            this.AfterInitialize += new RoutedEventHandler(CadastroContasContabeis_AfterInitialize);
            
        }

        #endregion

        // pendente
        #region CadastroContasContabeis_KeyDown

        void CadastroContasContabeis_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.F3)
            {
                if (this.CheckModulePlusLicense())
                {
                    SinacorTextBox TxtContaCredito = null;
                    SinacorInt32TextBox TxtCodResumidoCredito = null;
                    SinacorInt32TextBox TxtDigResumidoCredito = null;
                    SinacorInt32TextBox TxtCodHistoricoCredito = null;
                    SinacorInt32TextBox TxtDigHistoricoCredito = null;
                    SinacorLabel lblCtaCreditoNome = null;
                    SinacorTextBox TxtContaDebito = null;
                    SinacorInt32TextBox TxtCodResumidoDebito = null;
                    SinacorInt32TextBox TxtDigResumidoDebito = null;
                    SinacorInt32TextBox TxtCodHistoricoDebito = null;
                    SinacorInt32TextBox TxtDigHistoricoDebito = null;
                    SinacorLabel lblCtaDebitoNome = null;

                    //campos novos
                    SinacorInt32TextBox TxtCodHistoricoEstorno = null;
                    SinacorInt32TextBox TxtDigHistoricoEstorno = null;

                    if (this.DtgContasContabeis.IsInserting || this.DtgContasContabeis.IsUpdating || this.DtgContasContabeis.IsEditing)
                    {
                        if (this.DtgContasContabeis.IsInserting)
                        {
                            TxtContaCredito = ((SinacorTextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtContaCredito"));
                            TxtCodResumidoCredito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtCodResumidoCredito"));
                            TxtDigResumidoCredito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtDigResumidoCredito"));
                            TxtCodHistoricoCredito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtCodHistoricoCredito"));
                            TxtDigHistoricoCredito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtDigHistoricoCredito"));
                            lblCtaCreditoNome = ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "lblCtaCreditoNome"));

                            TxtContaDebito = ((SinacorTextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtContaDebito"));
                            TxtCodResumidoDebito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtCodResumidoDebito"));
                            TxtDigResumidoDebito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtDigResumidoDebito"));
                            TxtCodHistoricoDebito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtCodHistoricoDebito"));
                            TxtDigHistoricoDebito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtDigHistoricoDebito"));
                            lblCtaDebitoNome = ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "lblCtaDebitoNome"));

                            TxtCodHistoricoEstorno = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtCodHistoricoEstorno"));
                            TxtDigHistoricoEstorno = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtDigHistoricoEstorno"));
                        }
                        else
                        {
                            TxtContaCredito = ((SinacorTextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtContaCredito"));
                            TxtCodResumidoCredito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtCodResumidoCredito"));
                            TxtDigResumidoCredito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtDigResumidoCredito"));
                            TxtCodHistoricoCredito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtCodHistoricoCredito"));
                            TxtDigHistoricoCredito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtDigHistoricoCredito"));
                            lblCtaCreditoNome = ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "lblCtaCreditoNome"));

                            TxtContaDebito = ((SinacorTextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtContaDebito"));
                            TxtCodResumidoDebito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtCodResumidoDebito"));
                            TxtDigResumidoDebito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtDigResumidoDebito"));
                            TxtCodHistoricoDebito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtCodHistoricoDebito"));
                            TxtDigHistoricoDebito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtDigHistoricoDebito"));
                            lblCtaDebitoNome = ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "lblCtaDebitoNome"));

                            TxtCodHistoricoEstorno = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtCodHistoricoEstorno"));
                            TxtDigHistoricoEstorno = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtDigHistoricoEstorno"));
                        }

                        if (TxtContaCredito.IsFocused || TxtCodResumidoCredito.IsFocused)
                        {
                            Model.ContaContabil contaContabil = null;

                            if (TxtContaCredito.IsFocused)
                                contaContabil = this.ChamarTelaPesquisaContasContabeis(null, TxtContaCredito.Text, string.Empty);
                            else if (TxtCodResumidoCredito.IsFocused)
                                contaContabil = this.ChamarTelaPesquisaContasContabeis(TxtCodResumidoCredito.Value, TxtContaCredito.Text, (lblCtaCreditoNome.Content == null) ? "" : lblCtaCreditoNome.Content.ToString());

                            if (contaContabil != null && contaContabil.NumeroConta != null)
                            {
                                TxtContaCredito.Text = contaContabil.NumeroConta;
                                TxtCodResumidoCredito.Value = contaContabil.CodigoResumido;
                                TxtDigResumidoCredito.Value = contaContabil.Digito;
                                lblCtaCreditoNome.Content = contaContabil.NomeConta;
                            }
                        }
                        else if (TxtContaDebito.IsFocused || TxtCodResumidoDebito.IsFocused)
                        {
                            Model.ContaContabil contaContabil = null;

                            if (TxtContaDebito.IsFocused)
                                contaContabil = this.ChamarTelaPesquisaContasContabeis(null, TxtContaDebito.Text, string.Empty);
                            else if (TxtCodResumidoDebito.IsFocused)
                                contaContabil = this.ChamarTelaPesquisaContasContabeis(TxtCodResumidoDebito.Value, TxtContaDebito.Text, (lblCtaDebitoNome.Content == null) ? "" : lblCtaDebitoNome.Content.ToString());

                            if (contaContabil != null && contaContabil.NumeroConta != null)
                            {
                                TxtContaDebito.Text = contaContabil.NumeroConta;
                                TxtCodResumidoDebito.Value = contaContabil.CodigoResumido;
                                TxtDigResumidoDebito.Value = contaContabil.Digito;
                                lblCtaDebitoNome.Content = contaContabil.NomeConta;
                            }
                        }
                        else if (TxtCodHistoricoCredito.IsFocused)
                        {
                            Model.HistoricoContabil historicoContabil = new Model.HistoricoContabil();
                            if (TxtCodHistoricoCredito.Value.HasValue)
                                historicoContabil.Codigo = TxtCodHistoricoCredito.Value.Value;

                            if (TxtDigHistoricoCredito.Value.HasValue)
                            {
                                historicoContabil.Digito = TxtDigHistoricoCredito.Value.Value;
                                historicoContabil = this.ChamarTelaPesquisaHistoricosContabilidade(historicoContabil);
                            }
                            else
                                historicoContabil = this.ChamarTelaPesquisaHistoricosContabilidade(null);

                            if (historicoContabil != null && historicoContabil.Codigo.HasValue)
                            {
                                TxtCodHistoricoCredito.Value = historicoContabil.Codigo;
                                TxtDigHistoricoCredito.Value = historicoContabil.Digito;
                            }
                        }
                        else if (TxtCodHistoricoDebito.IsFocused)
                        {
                            Model.HistoricoContabil historicoContabil = new Model.HistoricoContabil();

                            if (TxtCodHistoricoDebito.Value.HasValue)
                                historicoContabil.Codigo = TxtCodHistoricoDebito.Value.Value;
                            else
                                historicoContabil.Codigo = null;
                            if (TxtDigHistoricoDebito.Value.HasValue)
                            {
                                historicoContabil.Digito = TxtDigHistoricoDebito.Value.Value;
                                historicoContabil = this.ChamarTelaPesquisaHistoricosContabilidade(historicoContabil);
                            }
                            else
                                historicoContabil = this.ChamarTelaPesquisaHistoricosContabilidade(null);

                            if (historicoContabil != null && historicoContabil.Codigo.HasValue)
                            {
                                TxtCodHistoricoDebito.Value = historicoContabil.Codigo;
                                TxtDigHistoricoDebito.Value = historicoContabil.Digito;
                            }
                        }
                        else if(TxtCodHistoricoEstorno.IsFocused || TxtDigHistoricoEstorno.IsFocused)
                        {
                            Model.HistoricoContabil historicoContabil = null;

                            if (TxtCodHistoricoEstorno.Value.HasValue && TxtDigHistoricoEstorno.Value.HasValue)
                            {
                                historicoContabil = new Model.HistoricoContabil();
                                historicoContabil.Codigo = TxtCodHistoricoEstorno.Value.Value;
                                historicoContabil.Digito = TxtDigHistoricoEstorno.Value.Value;
                            }

                            historicoContabil = this.ChamarTelaPesquisaHistoricosContabilidade(historicoContabil);

                            if (historicoContabil != null && historicoContabil.Codigo.HasValue)
                            {
                                TxtCodHistoricoEstorno.Value = historicoContabil.Codigo;
                                TxtDigHistoricoEstorno.Value = historicoContabil.Digito;
                            }
                        }
                        
                    }
                }
            }
        }

        private Model.ContaContabil ChamarTelaPesquisaContasContabeis(Int32? codigoResumido, String numeroConta, String descricaoConta)
        {
            PesquisaContasContabeis telaPesquisaContasContabeis = new PesquisaContasContabeis(codigoResumido, numeroConta, descricaoConta);

            telaPesquisaContasContabeis.MenuHeader = new List<String>();
            telaPesquisaContasContabeis.MenuHeader.Add("RESOURCEKEY");
            telaPesquisaContasContabeis.Owner = this;
            telaPesquisaContasContabeis.SetMenuHeader("Bovespa.Contabil.Label.PesquisaCCNomeTela");
            telaPesquisaContasContabeis.ShowDialog();

            return telaPesquisaContasContabeis.pesquisaContasContabeisPresenter.ContaContabilSelecionada;
        }

        private Model.HistoricoContabil ChamarTelaPesquisaHistoricosContabilidade(Model.HistoricoContabil historico)
        {
            PesquisaHistoricosContabilidade telaPesquisaHistóricosContabeis;
            if (historico != null)
                telaPesquisaHistóricosContabeis = new PesquisaHistoricosContabilidade(historico);
            else
                telaPesquisaHistóricosContabeis = new PesquisaHistoricosContabilidade();

            telaPesquisaHistóricosContabeis.MenuHeader = new List<String>();
            telaPesquisaHistóricosContabeis.MenuHeader.Add("RESOURCEKEY");
            telaPesquisaHistóricosContabeis.SetMenuHeader("Bovespa.Contabil.Tooltip.PesquisaHistCCNomeTela");
            telaPesquisaHistóricosContabeis.Owner = this;

            telaPesquisaHistóricosContabeis.ShowDialog();

            return telaPesquisaHistóricosContabeis.Historico;
        }

        #endregion

        #region CadastroContasContabeis_AfterInitialize

        void CadastroContasContabeis_AfterInitialize(object sender, RoutedEventArgs e)
        {
            this.PresenterCadastroContaContabilFaturamento = new CadastroContaContabilFaturamento(this);

            this.DataContext = this.PresenterCadastroContaContabilFaturamento;

            this.DtgContasContabeis.Deleting += new CancelItemsEventHandler(DtgContasContabeis_Deleting);
            this.DtgContasContabeis.InsertBegun += new ItemEventHandler(DtgContasContabeis_InsertBegun);
            this.DtgContasContabeis.UpdateBegun += new ItemEventHandler(DtgContasContabeis_UpdateBegun);
            this.DtgContasContabeis.EndingUpdate += new CancelItemEventHandler(DtgContasContabeis_EndingUpdate);
            this.DtgContasContabeis.EndingInsert += new CancelItemEventHandler(DtgContasContabeis_EndingInsert);
            this.DtgContasContabeis.CancelingInsert += new CancelItemEventHandler(DtgContasContabeis_CancelingInsert);
            this.DtgContasContabeis.CancelingUpdate += new CancelItemEventHandler(DtgContasContabeis_CancelingUpdate);

            this.CmbEventoContabil.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(CmbEventoContabil_SelectionChanged);

            this.BtnPesquisar.Click += new RoutedEventHandler(BtnPesquisar_Click);
            this.BtnNovaPesquisa.Click += new RoutedEventHandler(BtnNovaPesquisa_Click);

            this.PresenterCadastroContaContabilFaturamento.Inicializar();
        }

        #endregion

        // Métodos Auxiliares

        #region FiltroConsultaContasContabeis

        private void FiltroConsultaContasContabeis()
        {
            this.PresenterCadastroContaContabilFaturamento.FiltroContasContabeis = new Model.Parameter.FiltroContaContabilFaturamento()
            {
                CodigoBolsa = this.CmbBolsa.SelectedValue == null ? null : this.CmbBolsa.SelectedValue.ToString(),
                CodigoEventoContabil = (Int32?)this.CmbEventoContabil.SelectedValue,
                CodigoGrupoContabil = (Int32?)this.CmbGrupoContabil.SelectedValue,
                CodigoMunicipio = (Int32?)this.CmbMunicipio.SelectedValue
            };
        }

        #endregion

        #region ContaContabilFaturamento

        private Model.ContaContabilFaturamento ContaContabilFaturamento()
        {
            return new Model.ContaContabilFaturamento()
            {
                CodigoBolsa = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoBolsa,
                CodigoEventoContabil = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoEventoContabil,
                CodigoGrupoContabil = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoGrupoContabil,
                CodigoHistoricoCredito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoHistoricoCredito,
                CodigoHistoricoDebito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoHistoricoDebito,
                CodigoHistoricoEstorno = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoHistoricoEstorno,
                CodigoMunicipio = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoMunicipio,
                CodigoResumidoCredito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoResumidoCredito,
                CodigoResumidoDebito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoResumidoDebito,
                ContaCredito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).ContaCredito,
                ContaDebito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).ContaDebito,
                DigitoHistoricoCredito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoHistoricoCredito,
                DigitoHistoricoDebito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoHistoricoDebito,
                DigitoHistoricoEstorno = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoHistoricoEstorno,
                DigitoResumidoCredito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoResumidoCredito,
                DigitoResumidoDebito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoResumidoDebito,
            };
        }

        #endregion

        #region CarregarContaCredito

        private void CarregarContaCredito(Model.Enum.TipoPesquisaConta conta)
        {
            this.PresenterCadastroContaContabilFaturamento.CarregarContaCredito
            (
                new Model.ContaContabilFaturamento()
                {
                    ContaCredito = ((this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento)).ContaCredito,
                    CodigoResumidoCredito = ((this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento)).CodigoResumidoCredito,
                    DigitoResumidoCredito = ((this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento)).DigitoResumidoCredito
                }, conta
            );

            if (string.IsNullOrEmpty((this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).ContaCredito))
            {
                ((SinacorLabel)ControlHelper.FindChild(this, "lblCtaCreditoNome")).Content = null;
            }
        }

        #endregion

        #region CarregarContaDebito

        private void CarregarContaDebito(Model.Enum.TipoPesquisaConta conta)
        {
            this.PresenterCadastroContaContabilFaturamento.CarregarContaDebito
            (
                new Model.ContaContabilFaturamento()
                {
                    ContaDebito = ((this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento)).ContaDebito,
                    CodigoResumidoDebito = ((this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento)).CodigoResumidoDebito,
                    DigitoResumidoDebito = ((this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento)).DigitoResumidoDebito
                }, conta
            );

            if (string.IsNullOrEmpty((this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).ContaDebito))
            {
                if (this.DtgContasContabeis.IsInserting)
                {
                    ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "lblCtaDebitoNome")).Content = null;
                }
                else
                {
                    ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "lblCtaDebitoNome")).Content = null;
                }
            }
        }

        #endregion

        // Controles XAML

        #region CmbEventoContabil_SelectionChanged

        void CmbEventoContabil_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            this._isSelectedEventoGrid = false;
            if (this.CmbEventoContabil.SelectedItem != null)
                this.PresenterCadastroContaContabilFaturamento.ValidarUtilizacaoMunicipio(this.CmbEventoContabil.SelectedItem == null ? null : (Model.EventoContabil)this.CmbEventoContabil.SelectedItem);
        }

        #endregion

        #region BtnPesquisar_Click

        void BtnPesquisar_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.ShowUserWaitingProcess();
                this.FiltroConsultaContasContabeis();
                this.PresenterCadastroContaContabilFaturamento.ConsultarContasContabeis();
            }
            finally
            {
                this.HideUserWaitingProcess();
            }
        }

        #endregion

        #region BtnNovaPesquisa_Click

        void BtnNovaPesquisa_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.ShowUserWaitingProcess();

                if (this.DtgContasContabeis.IsInserting)
                    this.DtgContasContabeis.EndInsert();
                else if (this.DtgContasContabeis.IsEditing)
                    this.DtgContasContabeis.EndUpdate();

                if (this._permitirNovaPesquisa)
                {
                    this.CmbBolsa.SelectedValue = null;
                    this.CmbEventoContabil.SelectedValue = null;
                    this.CmbGrupoContabil.SelectedValue = null;
                    this.CmbMunicipio.SelectedValue = null;

                    this.FiltroConsultaContasContabeis();

                    PresenterCadastroContaContabilFaturamento.ListaContasContabeis.Clear();
                    DtgContasContabeis.UpdateTarget(SinacorDataGrid.ItemsSourceProperty);
                }
            }
            finally
            {
                this.HideUserWaitingProcess();
            }
        }

        #endregion

        #region DtgContasContabeis_Deleting

        void DtgContasContabeis_Deleting(object sender, CancelItemsEventArgs e)
        {
            try
            {
                this.ShowUserWaitingProcess();
                if (!this.PresenterCadastroContaContabilFaturamento.ExcluirContaContabilFaturamento((Model.ContaContabilFaturamento)this.DtgContasContabeis.SelectedItem))
                {
                    e.Cancel = true;
                }
            }
            finally
            {
                this.HideUserWaitingProcess();
            }
        }

        #endregion

        #region DtgContasContabeis_InsertBegun

        void DtgContasContabeis_InsertBegun(object sender, ItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                this._permitirNovaPesquisa = false;

                if (this.DtgContasContabeis.IsInserting)
                {
                    ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "lblCtaCreditoNome")).Content = null;
                    ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "lblCtaDebitoNome")).Content = null;
                }
                else
                {
                    ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "lblCtaCreditoNome")).Content = null;
                    ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "lblCtaDebitoNome")).Content = null;
                }                
            }
        }

        #endregion

        #region DtgContasContabeis_UpdateBegun

        void DtgContasContabeis_UpdateBegun(object sender, ItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                this._permitirNovaPesquisa = false;

                if (this.DtgContasContabeis.IsInserting)
                {
                    ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "CmbBolsaGrid")).IsEnabled = false;
                    ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "CmbEventoContabilGrid")).IsEnabled = false;
                    ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "CmbGrupoContabilGrid")).IsEnabled = false;
                    ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "CmbMunicipioGrid")).IsEnabled = false;
                    ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtCodResumidoCredito")).Focus();
                }
                else
                {
                    ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "CmbBolsaGrid")).IsEnabled = false;
                    ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "CmbEventoContabilGrid")).IsEnabled = false;
                    ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "CmbGrupoContabilGrid")).IsEnabled = false;
                    ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "CmbMunicipioGrid")).IsEnabled = false;
                    ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtCodResumidoCredito")).Focus();

                    ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "CmbMunicipioGrid")).SelectedValue = PresenterCadastroContaContabilFaturamento.ListaContasContabeis[this.DtgContasContabeis.EditIndex].CodigoMunicipio;
                    ((Model.ContaContabilFaturamento)this.DtgContasContabeis.EditItem).DescricaoMunicipio = PresenterCadastroContaContabilFaturamento.ListaContasContabeis[this.DtgContasContabeis.EditIndex].DescricaoMunicipio;
                }

                this.CarregarContaCredito(Model.Enum.TipoPesquisaConta.DigitoResumido);
                this.CarregarContaDebito(Model.Enum.TipoPesquisaConta.DigitoResumido);

                //this.PresenterCadastroContaContabilFaturamento.CarregarContaCredito
                //(
                //    new Model.ContaContabilFaturamento()
                //    {
                //        ContaCredito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).ContaCredito
                //    }, Model.Enum.TipoPesquisaConta.Conta
                //);

                //this.PresenterCadastroContaContabilFaturamento.CarregarContaDebito
                //(
                //    new Model.ContaContabilFaturamento()
                //    {
                //        ContaDebito = (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).ContaDebito
                //    }, Model.Enum.TipoPesquisaConta.Conta
                //);
            }
        }

        #endregion

        #region DtgContasContabeis_EndingInsert

        void DtgContasContabeis_EndingInsert(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                try
                {
                    this.ShowUserWaitingProcess();

                    this._permitirNovaPesquisa = true;

                    if (!this.ValidarCampos())
                        e.Cancel = true;

                    if (!this.HasErrors)
                    {
                        if (!this.PresenterCadastroContaContabilFaturamento.IncluirContaContabilFaturamento(this.ContaContabilFaturamento()))
                        {
                            e.Cancel = true;
                            this._permitirNovaPesquisa = false;
                        }
                    }
                }
                finally
                {
                    this.HideUserWaitingProcess();
                }
            }
        }

        #endregion

        #region DtgContasContabeis_EndingUpdate

        void DtgContasContabeis_EndingUpdate(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                try
                {
                    this.ShowUserWaitingProcess();

                    this._permitirNovaPesquisa = true;

                    if (!this.ValidarCampos())
                        e.Cancel = true;

                    if (!this.HasErrors)
                    {
                        if (!this.PresenterCadastroContaContabilFaturamento.AlterarContaContabilFaturamento(this.ContaContabilFaturamento()))
                        {
                            e.Cancel = true;
                            this._permitirNovaPesquisa = false;
                        }
                    }
                }
                finally
                {
                    this.HideUserWaitingProcess();
                }
            }
        }

        #endregion

        #region DtgContasContabeis_CancelingUpdate

        void DtgContasContabeis_CancelingUpdate(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                this.ClearServiceErrors();
                this._permitirNovaPesquisa = true;
            }
        }

        #endregion

        #region DtgContasContabeis_CancelingInsert

        void DtgContasContabeis_CancelingInsert(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                this.ClearServiceErrors();
                this._permitirNovaPesquisa = true;
            }
        }

        #endregion

        #region CmbBolsaGrid_SelectionChanged

        private void CmbBolsaGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (this.DtgContasContabeis.IsInserting || this.DtgContasContabeis.IsEditing)
            {
                if (((SinacorComboBox)sender).SelectedItem != null)
                {
                    if (this.DtgContasContabeis.EditItem != null)
                    {
                        ((Model.ContaContabilFaturamento)this.DtgContasContabeis.EditItem).DescricaoBolsa = ((Model.Bolsa)((SinacorComboBox)sender).SelectedItem).Descricao;
                    }
                }
            }
        }

        #endregion

        #region CmbGrupoContabilGrid_SelectionChanged

        private void CmbGrupoContabilGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (this.DtgContasContabeis.IsInserting || this.DtgContasContabeis.IsEditing)
            {
                if (((SinacorComboBox)sender).SelectedItem != null)
                {
                    if (this.DtgContasContabeis.EditItem != null)
                    {
                        ((Model.ContaContabilFaturamento)this.DtgContasContabeis.EditItem).DescricaoGrupoContabil = ((Model.GrupoContabil)((SinacorComboBox)sender).SelectedItem).Descricao;
                    }
                }
            }
        }

        #endregion

        #region CmbEventoContabilGrid_SelectionChanged

        private void CmbEventoContabilGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {

            if (this.DtgContasContabeis.IsInserting || this.DtgContasContabeis.IsEditing)
            {
                this._isSelectedEventoGrid = true;
                SinacorComboBox CmbEventoContabilGrid = (SinacorComboBox)sender;

                if (CmbEventoContabilGrid.SelectedItem != null)
                {
                    if (this.DtgContasContabeis.EditItem != null)
                    {
                        ((Model.ContaContabilFaturamento)this.DtgContasContabeis.EditItem).DescricaoEventoContabil = ((Model.EventoContabil)CmbEventoContabilGrid.SelectedItem).Descricao;
                    }
                }

                if (CmbEventoContabilGrid.SelectedItem != null)
                    this.PresenterCadastroContaContabilFaturamento.ValidarUtilizacaoMunicipio(CmbEventoContabilGrid.SelectedItem == null ? null : (Model.EventoContabil)CmbEventoContabilGrid.SelectedItem);
            }
        }

        #endregion

        #region CmbMunicipioGrid_SelectionChanged

        private void CmbMunicipioGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (this.DtgContasContabeis.IsInserting || this.DtgContasContabeis.IsEditing)
            {
                SinacorComboBox CmbMunicipioGrid = ((SinacorComboBox)sender);

                if (CmbMunicipioGrid.SelectedItem != null)
                {
                    if (this.DtgContasContabeis.EditItem != null)
                    {
                        ((Model.ContaContabilFaturamento)this.DtgContasContabeis.EditItem).DescricaoMunicipio = ((Model.Municipio)CmbMunicipioGrid.SelectedItem).Descricao;
                    }
                }

            }
        }

        #endregion

        #region TxtCodResumidoCredito_LostFocus

        private void TxtCodResumidoCredito_LostFocus(object sender, RoutedEventArgs e)
        {            
            this.CarregarContaCredito(Model.Enum.TipoPesquisaConta.DigitoResumido);
            this.ClearServiceErrors();
        }

        #endregion

        #region TxtDigResumidoCredito_LostFocus

        private void TxtDigResumidoCredito_LostFocus(object sender, RoutedEventArgs e)
        {
            this.CarregarContaCredito(Model.Enum.TipoPesquisaConta.DigitoResumido);
            this.ClearServiceErrors();
        }

        #endregion

        #region TxtDigResumidoDebito_LostFocus

        private void TxtDigResumidoDebito_LostFocus(object sender, RoutedEventArgs e)
        {
            this.CarregarContaDebito(Model.Enum.TipoPesquisaConta.DigitoResumido);
            this.ClearServiceErrors();
        }

        #endregion
        
        #region TxtCodResumidoDebito_LostFocus

        private void TxtCodResumidoDebito_LostFocus(object sender, RoutedEventArgs e)
        {
            this.CarregarContaDebito(Model.Enum.TipoPesquisaConta.DigitoResumido);
            this.ClearServiceErrors();
        }

        #endregion

        #region TxtContaCredito_LostFocus

        private void TxtContaCredito_LostFocus(object sender, RoutedEventArgs e)
        {
            this.CarregarContaCredito(Model.Enum.TipoPesquisaConta.Conta);
            this.ClearServiceErrors();
        }

        #endregion

        #region TxtContaDebito_LostFocus

        private void TxtContaDebito_LostFocus(object sender, RoutedEventArgs e)
        {
            this.CarregarContaDebito(Model.Enum.TipoPesquisaConta.Conta);
            this.ClearServiceErrors();
        }

        #endregion

        #region TxtDigHistoricoEstorno_LostFocus

        private void TxtDigHistoricoEstorno_LostFocus(object sender, RoutedEventArgs e)
        {
            this.PresenterCadastroContaContabilFaturamento.ValidarCodigoHistorico
            (
                (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoHistoricoEstorno,
                (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoHistoricoEstorno,
                Model.Enum.TipoHistorico.Estorno
            );
            this.ClearServiceErrors();
        }

        #endregion

        #region TxtDigHistoricoEstorno_LostFocus

        private void TxtDigHistoricoDebito_LostFocus(object sender, RoutedEventArgs e)
        {
            this.PresenterCadastroContaContabilFaturamento.ValidarCodigoHistorico
            (
                (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoHistoricoDebito,
                (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoHistoricoDebito,
                Model.Enum.TipoHistorico.Debito
            );
            this.ClearServiceErrors();

        }

        #endregion

        #region TxtDigHistoricoEstorno_LostFocus

        private void TxtDigHistoricoCredito_LostFocus(object sender, RoutedEventArgs e)
        {
            this.PresenterCadastroContaContabilFaturamento.ValidarCodigoHistorico
            (
                (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoHistoricoCredito,
                (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoHistoricoCredito,
                Model.Enum.TipoHistorico.Credito
            );
            this.ClearServiceErrors();
        }

        #endregion

        #region BtnCancelar_Click

        private void BtnCancelar_Click(object sender, RoutedEventArgs e)
        {
            if (this.DtgContasContabeis.IsInserting)
                this.DtgContasContabeis.CancelInsert();
            else if (this.DtgContasContabeis.IsEditing)
                this.DtgContasContabeis.CancelUpdate();
        }

        #endregion

        #region BtnSalvar_Click

        private void BtnSalvar_Click(object sender, RoutedEventArgs e)
        {
            if (this.DtgContasContabeis.IsInserting)
                this.DtgContasContabeis.EndInsert();
            else if (this.DtgContasContabeis.IsEditing)
                this.DtgContasContabeis.EndUpdate();
        }

        #endregion

        /* Retorno da Presenter */

        #region AtualizarBolsas

        public void AtualizarBolsas()
        {
            this.CmbBolsa.UpdateTarget(SinacorComboBox.ItemsSourceProperty);
        }

        #endregion

        #region AtualizarMunicipios

        public void AtualizarMunicipios()
        {
            this.CmbMunicipio.UpdateTarget(SinacorComboBox.ItemsSourceProperty);
        }

        #endregion

        #region AtualizarEventosContabeis

        public void AtualizarEventosContabeis()
        {
            this.CmbEventoContabil.UpdateTarget(SinacorComboBox.ItemsSourceProperty);
        }

        #endregion

        #region AtualizarGruposContabeis

        public void AtualizarGruposContabeis()
        {
            this.CmbGrupoContabil.UpdateTarget(SinacorComboBox.ItemsSourceProperty);
        }

        #endregion

        #region AtualizarContasContabeisFaturamento

        public void AtualizarContasContabeisFaturamento()
        {
            this.DtgContasContabeis.UpdateTarget(SinacorDataGrid.ItemsSourceProperty);
        }

        #endregion

        #region HabilitarConfiguracaoMunicipio

        public void HabilitarConfiguracaoMunicipio()
        {
            if (this._isSelectedEventoGrid)
            {
                if (this.DtgContasContabeis.IsInserting)
                {
                    ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "LblMunicipioGrid")).IsTargetFieldRequired = true;

                    SinacorComboBox CmbMunicipioGrid = ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "CmbMunicipioGrid"));
                    CmbMunicipioGrid.IsEnabled = true;
                    CmbMunicipioGrid.IsRequired = true;
                }
                else
                {
                    ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "LblMunicipioGrid")).IsTargetFieldRequired = true;

                    SinacorComboBox CmbMunicipioGrid = ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "CmbMunicipioGrid"));
                    CmbMunicipioGrid.IsEnabled = true;
                    CmbMunicipioGrid.IsRequired = true;
                }
            }
            else
                this.CmbMunicipio.IsEnabled = true;
        }

        #endregion

        #region DesabilitarConfiguracaoMunicipio

        public void DesabilitarConfiguracaoMunicipio()
        {
            if (this._isSelectedEventoGrid)
            {
                if (this.DtgContasContabeis.IsInserting)
                {
                    SinacorLabel LblMunicipioGrid = (SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "LblMunicipioGrid");
                    if (LblMunicipioGrid!= null)
	                {
                        LblMunicipioGrid.IsTargetFieldRequired = false;
	                }                   

                    SinacorComboBox CmbMunicipioGrid = ((SinacorComboBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "CmbMunicipioGrid"));
                    Model.ContaContabilFaturamento model = (Model.ContaContabilFaturamento)this.DtgContasContabeis.EditItem;
                    if (CmbMunicipioGrid != null)
                    {
                        CmbMunicipioGrid.IsEnabled = false;
                        CmbMunicipioGrid.IsRequired = false;
                        CmbMunicipioGrid.SelectedIndex = -1;
                        model.CodigoMunicipio = null;
                        model.DescricaoMunicipio = String.Empty;
                    }                    
                }
                else
                {
                    Row row = this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex);

                    if (row != null)
	                {
                        SinacorLabel LblMunicipioGrid = (SinacorLabel)ControlHelper.FindChild(row, "LblMunicipioGrid");
                        if (LblMunicipioGrid != null)
                        {
                            LblMunicipioGrid.IsTargetFieldRequired = false;
                        }

                        SinacorComboBox CmbMunicipioGrid = ((SinacorComboBox)ControlHelper.FindChild(row, "CmbMunicipioGrid"));
                        Model.ContaContabilFaturamento model = (Model.ContaContabilFaturamento)this.DtgContasContabeis.EditItem;
                        if (CmbMunicipioGrid != null)
                        {
                            CmbMunicipioGrid.IsEnabled = false;
                            CmbMunicipioGrid.IsRequired = false;
                            CmbMunicipioGrid.SelectedIndex = -1;
                            model.CodigoMunicipio = null;
                            model.DescricaoMunicipio = String.Empty;
                        } 
	                }                    
                }
            }
            else
            {
                this.CmbMunicipio.SelectedValue = null;
                this.CmbMunicipio.IsEnabled = false;
            }
        }

        #endregion

        #region InformarMunicipioObrigatorio

        public void InformarMunicipioObrigatorio()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.ExceptionMessage.MunicipioObrigatorio").ToString(), SinacorMessageBoxImage.Exclamation);
        }

        #endregion

        #region InformarCodigoHistoricoCreditoObrigatorio

        public void InformarCodigoHistoricoCreditoObrigatorio()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.ExceptionMessage.CodigoHistoricoCreditoObrigatorio").ToString(), SinacorMessageBoxImage.Exclamation);
        }

        #endregion

        #region InformarCodigoHistoricoCreditoInvalido

        public void InformarCodigoHistoricoCreditoInvalido()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.Message.CodigoHistoricoInvalido").ToString(), SinacorMessageBoxImage.Exclamation);
        }

        #endregion

        #region InformarCodigoHistoricoDebitoInvalido

        public void InformarCodigoHistoricoDebitoInvalido()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.Message.CodigoHistoricoInvalido").ToString(), SinacorMessageBoxImage.Exclamation);
        }

        #endregion

        #region InformarCodigoHistoricoDebitoObrigatorio

        public void InformarCodigoHistoricoDebitoObrigatorio()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.ExceptionMessage.CodigoHistoricoDebitoObrigatorio").ToString(), SinacorMessageBoxImage.Exclamation);
        }

        #endregion

        #region InformarCodigoHistoricoEstornoObrigatorio

        public void InformarCodigoHistoricoEstornoObrigatorio()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.ExceptionMessage.CodigoHistoricoEstornoObrigatorio").ToString(), SinacorMessageBoxImage.Exclamation);
        }

        #endregion

        #region InformarCodigoHistoricoEstornoInvalido

        public void InformarCodigoHistoricoEstornoInvalido()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.Message.CodigoHistoricoEstornoInvalido").ToString(), SinacorMessageBoxImage.Exclamation);
        }

        #endregion

        #region InformarContaContabilAlteradaSucesso

        public void InformarContaContabilAlteradaSucesso()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.Message.ContaContabilAtualizadaSucesso").ToString(), SinacorMessageBoxImage.Information);
        }

        #endregion

        // pendente
        #region InformarDadosContaCreditoPorContaCredito

        public void InformarDadosContaCreditoPorContaCredito(Int32? codigoResumido, Byte? digitoResumido, String nomeConta)
        {
            (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoResumidoCredito = codigoResumido;
            (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoResumidoCredito = digitoResumido;

            if (this.DtgContasContabeis.IsInserting)
            {
                ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "lblCtaCreditoNome")).Content = nomeConta;
            }
            else
            {
                ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "lblCtaCreditoNome")).Content = nomeConta;
            }
            
        }

        #endregion

        // pendente
        #region InformarDadosContaCreditoPorDigitoResumido

        public void InformarDadosContaCreditoPorDigitoResumido(String codigoConta, Byte? digitoResumido, String nomeConta)
        {
            (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).ContaCredito = codigoConta;
            (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoResumidoCredito = digitoResumido;

            if (this.DtgContasContabeis.IsInserting)
            {
                ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "lblCtaCreditoNome")).Content = nomeConta;
            }
            else
            {
                ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "lblCtaCreditoNome")).Content = nomeConta;
            }
        }

        #endregion

        // pendente
        #region InformarDadosContaDebitoPorContaDebito

        public void InformarDadosContaDebitoPorContaDebito(Int32? codigoResumido, Byte? digitoResumido, String nomeConta)
        {
            (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).CodigoResumidoDebito = codigoResumido;
            (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoResumidoDebito= digitoResumido;

            if (this.DtgContasContabeis.IsInserting)
            {
                ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "lblCtaDebitoNome")).Content = nomeConta;
            }
            else
            {
                ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "lblCtaDebitoNome")).Content = nomeConta;
            }
        }

        #endregion

        // pendente
        #region InformarDadosContaDebitoPorDigitoResumido

        public void InformarDadosContaDebitoPorDigitoResumido(String codigoConta, Byte? digitoResumido, String nomeConta)
        {
            (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).ContaDebito = codigoConta;
            (this.DtgContasContabeis.EditItem as Model.ContaContabilFaturamento).DigitoResumidoDebito = digitoResumido;

            if (this.DtgContasContabeis.IsInserting)
            {
                ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "lblCtaDebitoNome")).Content = nomeConta;
            }
            else
            {
                ((SinacorLabel)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "lblCtaDebitoNome")).Content = nomeConta;
            }
        }

        #endregion

        #region InformarCodigoResumidoContaCredito

        public void InformarCodigoResumidoContaCredito()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.ExceptionMessage.CodigoResumidoContaCreditoObrigatorio").ToString(), SinacorMessageBoxImage.Exclamation);

            if (this.DtgContasContabeis.IsInserting)
            {
                ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtCodResumidoCredito")).Focus();
            }
            else
            {
                ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtCodResumidoCredito")).Focus();
            }
        }

        #endregion

        #region InformarCodigoResumidoContaDebito

        public void InformarCodigoResumidoContaDebito()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.ExceptionMessage.CodigoResumidoContaDebitoObrigatorio").ToString(), SinacorMessageBoxImage.Exclamation);

            if (this.DtgContasContabeis.IsInserting)
            {
                ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtCodResumidoDebito")).Focus();
            }
            else
            {
                ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtCodResumidoDebito")).Focus();
            }
        }

        #endregion

        #region InformarSelecioneContaContabil

        public void InformarSelecioneContaContabil()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.ExceptionMessage.SelecioneContaContabil").ToString(), SinacorMessageBoxImage.Exclamation);
        }

        #endregion

        #region ConfirmarExclusaoContaContabil

        public Boolean ConfirmarExclusaoContaContabil()
        {
            return SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.Message.DesejaExcluirContaContabil").ToString(), SinacorMessageBoxButton.YesNo, SinacorMessageBoxImage.Question) == SinacorMessageBoxResult.Yes ? true : false;
        }

        #endregion

        #region InformarExclusaoSucesso

        public void InformarExclusaoSucesso()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.Message.ContaContabilExcluidaSucesso").ToString(), SinacorMessageBoxImage.Information);
        }

        #endregion

        #region InformarContaContabilIncluidaSucesso

        public void InformarContaContabilIncluidaSucesso()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Contabil.Message.ContaContabilIncluidaSucesso").ToString(), SinacorMessageBoxImage.Information);
        }

        #endregion

        #region ValidarCampos

        private bool ValidarCampos()
        {
            bool retorno = true;
            SinacorInt32TextBox TxtCodResumidoCredito = null;
            SinacorTextBox TxtContaCredito = null;
            SinacorInt32TextBox TxtCodResumidoDebito = null;
            SinacorTextBox TxtContaDebito = null;


            if (this.DtgContasContabeis.IsInserting)
            {
                TxtCodResumidoCredito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtCodResumidoCredito"));
                TxtCodResumidoCredito.IsRequired = false;
                TxtContaCredito = ((SinacorTextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtContaCredito"));
                TxtContaCredito.IsRequired = false;
                TxtCodResumidoDebito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtCodResumidoDebito"));
                TxtCodResumidoDebito.IsRequired = false;
                TxtContaDebito = ((SinacorTextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetInsertionRow(), "TxtContaDebito"));
                TxtContaDebito.IsRequired = false;
            }
            else
            {
                TxtCodResumidoCredito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtCodResumidoCredito"));
                TxtCodResumidoCredito.IsRequired = false;
                TxtContaCredito = ((SinacorTextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtContaCredito"));
                TxtContaCredito.IsRequired = false;
                TxtCodResumidoDebito = ((SinacorInt32TextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtCodResumidoDebito"));
                TxtCodResumidoDebito.IsRequired = false;
                TxtContaDebito = ((SinacorTextBox)ControlHelper.FindChild(this.DtgContasContabeis.GetRow(this.DtgContasContabeis.EditIndex), "TxtContaDebito"));
                TxtContaDebito.IsRequired = false;
            }


            if (!this.CurrentModel.CodigoResumidoCredito.HasValue)
            {
                TxtCodResumidoCredito.IsRequired = true;
                TxtCodResumidoCredito.Validate();
                retorno = false;
            }
            else if (string.IsNullOrEmpty(this.CurrentModel.ContaCredito))
            {
                TxtContaCredito.IsRequired = true;
                TxtContaCredito.Validate();
                retorno = false;
            }
            else if (!this.CurrentModel.CodigoResumidoDebito.HasValue)
            {
                TxtCodResumidoDebito.IsRequired = true;
                TxtCodResumidoDebito.Validate();
                retorno = false;
            }
            else if (string.IsNullOrEmpty(this.CurrentModel.ContaDebito))
            {
                TxtContaDebito.IsRequired = true;
                TxtContaDebito.Validate();
                retorno = false;
            }

            return retorno;
        }
        #endregion

    }
}
